package com.mall.shopping.services;

import com.mall.shopping.IItemService;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: gateway
 * @description:
 * @author: Liu
 * @create: 2021-08-25 11:06
 **/
@Slf4j
@Service
public class ItemServiceImpl implements IItemService {

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductCateConverter productCateConverter;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    /**
     * 查看商品分类
     */
    @Override
    public ProductCateResponse itemCat() {
        ProductCateResponse productCateResponse = new ProductCateResponse();
        try {
//            // 判断排序
//            String orderCol = "sort_order";
//            String orderDir = "asc";
//            if(request.getSort().equals("1")){
//                orderCol="sort_order";
//                orderDir="asc";
//            }else if(request.getSort().equals("-1")){
//                orderCol="sort_order";
//                orderDir="desc";
//            }
//            // 构造排序
//            Example itemCatExample = new Example(ItemCat.class);
//            itemCatExample.setOrderByClause(orderCol + " " + orderDir);

            // 查询
//            List<ItemCat> itemCats = itemCatMapper.selectByExample(itemCatExample);
            List<ItemCat> itemCats = itemCatMapper.selectAll();
            // 转化
            List<ProductCateDto> productCateDtoList = productCateConverter.items2Dto(itemCats);

            productCateResponse.setProductCateDtoList(productCateDtoList);
            productCateResponse.setCode("000000");
            productCateResponse.setMsg("success");
        } catch (Exception e) {
            log.error("CategoriesServiceImpl.itemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(productCateResponse,e);
        }
        return productCateResponse;
    }

    @Override
    public ProductDetailResponse itemDetail(ProductDetailRequest productDetailRequest) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            Long id = productDetailRequest.getId();
            // 数据库查找
            Item item = itemMapper.selectByPrimaryKey(id);
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);

            ProductDetailDto productDetailDto = new ProductDetailDto();
            productDetailDto.setProductId(id);
            productDetailDto.setProductName(item.getTitle());
            productDetailDto.setLimitNum(Long.valueOf(item.getLimitNum()));
            productDetailDto.setProductImageBig(item.getImageBig());
            if(StringUtils.isNotBlank(item.getImage())){
                String images[]=item.getImage().split(",");
                productDetailDto.setProductImageBig(images[0]);
                productDetailDto.setProductImageSmall(Arrays.asList(images));
            }
            productDetailDto.setSalePrice(item.getPrice());
            productDetailDto.setDetail(itemDesc.getItemDesc());

            response.setProductDetailDto(productDetailDto);
            response.setCode("000000");
            response.setMsg("success");

        } catch (Exception e) {
            log.error("CategoriesServiceImpl.itemCat occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }

        return response;
    }
}
